<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增采购订单明细')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-detail-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">采购订单：</label>
                <div class="col-sm-8">
                    <input name="orderId" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">商品：</label>
                <div class="col-sm-8">
                    <select name="productId" id="productId" class="form-control select2-hidden-accessible" required>
                        <option value="">请选择商品</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">采购数量：</label>
                <div class="col-sm-8">
                    <input name="quantity" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">采购单价：</label>
                <div class="col-sm-8">
                    <input name="unitPrice" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">明细总价：</label>
                <div class="col-sm-8">
                    <input name="totalPrice" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">实际到货数量：</label>
                <div class="col-sm-8">
                    <input name="receivedQuantity" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea name="remark" class="form-control"></textarea>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var prefix = ctx + "purchase/detail";
        var baseProductPrefix = ctx + "basedata/product";
        
        $("#form-detail-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-detail-add').serialize());
            }
        }
        
        $(function() {
            // 初始化商品下拉框
            $.ajax({
                type: "get",
                url: prefix + "/productList",
                dataType: 'json',
                success: function(result) {
                    var products = result.data;
                    var options = "";
                    $.each(products, function(index, product) {
                        options += '<option value="' + product.productId + '" data-price="' + product.unitPrice + '">' + product.productName + '</option>';
                    });
                    $("select[name='productId']").append(options);
                }
            });
            
            // 初始化select2
            $(".select2-hidden-accessible").select2({
                placeholder: "请选择",
                allowClear: true
            });
            
            // 商品选择变化时，自动填充单价
            $("#productId").change(function() {
                var selectedOption = $(this).find(":selected");
                var unitPrice = selectedOption.data("price");
                if (unitPrice) {
                    $("input[name='unitPrice']").val(unitPrice);
                    // 触发单价变化事件，自动计算总价
                    $("input[name='unitPrice']").trigger("change");
                }
            });
            
            // 单价和数量变化时自动计算总价
            $("input[name='unitPrice'], input[name='quantity']").change(function() {
                var unitPrice = $("input[name='unitPrice']").val();
                var quantity = $("input[name='quantity']").val();
                if (unitPrice && quantity) {
                    var totalPrice = parseFloat(unitPrice) * parseFloat(quantity);
                    $("input[name='totalPrice']").val(totalPrice.toFixed(2));
                }
            });
        });
    </script>
</body>
</html>